﻿package com.xuanque.mahjongDice.display{
	
	import datas.Player;
	
	import flash.display.*;
	import flash.events.*;
	import utils.transitions.*;
	
	import com.xuanque.mahjongDice.Main;
	
	public class ResultCard extends Sprite{
		
		private var contentList:Sprite;
		private var list_length:uint;
		private var _width:Number;
		private var isTween:Boolean;
		private var _dis:uint = 22;
		
		public function ResultCard(){
			this._width = this.width;
			
			contentList = new Sprite();
			this.contentList.x = 5;
			this.contentList.y = -28;
			addChild(this.contentList);
			
			
			addListener();
		}
		
		private function addListener():void{
			this.ResultTip_btn.addEventListener(MouseEvent.CLICK,ResultTipHandler);
			
		}
		
		
		private function ResultTipHandler(event:MouseEvent):void{
			if(this.isTween == false){
				Tweener.addTween(this,{x: -this._width+10,time:.5});
			}else{
				Tweener.addTween(this,{x:-9,time:.5});
			}
			this.isTween = !this.isTween;
		}
		
		
		
		/*
		 * @列表添加：addPlayerUser(_name:String,prev_grade:String,all_grade:String)
		 * 
		*/
		public function addPlayerUser(player:Player):void {
			
			var item:ResultItem = new ResultItem(player);
			
			list_length = this.contentList.numChildren;
			if (list_length > 0) {
				var newItem:ResultItem = contentList.getChildAt(list_length - 1) as ResultItem;
				item.y = newItem.y + this._dis;
			}
			
			this.contentList.addChild(item);
			
			this.refresh();
		}
		
		/*
		 * @ updateResult
		 * 
		*/
		public function updateUserResult( result_array:Array ):void {
			
			//Main.getInstance().infoPanel.chatWindow.joinText( "本局结果，结果统计:" ,  "" ,"" );//更新 聊天窗口
			
			this.list_length = this.contentList.numChildren;
			var newItem:ResultItem;
			for (var i = 0; i < this.list_length; i++) {
				
				newItem = this.getThisPlayer( result_array[i][0] , this.list_length );
				
				newItem.setPrevSeed(result_array[i][1]);
				
				//Main.getInstance().infoPanel.chatWindow.joinText( result_array[i][1] + "" ,  result_array[i][0] ,"result" );//更新 聊天窗口
				
			}
			
			//Main.getInstance().infoPanel.chatWindow.joinText( "" ,  "" ,"null" );//更新 聊天窗口
			
			
		}
		
		/*
		 * 根据用户名取得 ResultItem
		*/
		private function getThisPlayer(username:String,_length:uint):ResultItem {
			
			var newItem:ResultItem;
			for (var i = 0; i < _length; i++ ) {
				
				newItem = contentList.getChildAt(i) as ResultItem;
				if ( newItem.getName() == username ) {
					
					return newItem;
				}
				
			}
			
			return null;
			
		}
		
		
		
		/*
		 * 
		 * @移除所有项
		*/
		public function removeAllItem():void {
			
			var count = this.contentList.numChildren;
			for (var i:int = 0; i <count; i++) {
				
				contentList.removeChildAt(0);
			}
			
		}
		
		
		
		
		
		/*
		 * 
		 * @移除某一用户：removeUser(player:Player)
		*/
		public function removeUser(player:Player):void {
			
			var name:String = player.getLoginName();
			
			this.removeItemAt(name);
			
		}
		
		
		
		/*
		 * 
		 * @移除列：removeItemAt(_text:String)
		*/
		public function removeItemAt(_name:String) {
			var count = contentList.numChildren;
			for (var i:int = 0; i <count; i++) {
				var item:ResultItem = contentList.getChildAt(i) as ResultItem;
				if (item.getName() == _name) {
					contentList.removeChild(item);
					//trace("removeItemAt  count = " + count);
					this.refresh();
					return;
				}
				
			}
			
		}
		
		private function refresh(){
			
			var count = contentList.numChildren;
			var newY:Number = - this._dis;
			for (var i:int = 0; i <count; i++) {
				var item:ResultItem = contentList.getChildAt(i) as ResultItem;
				Tweener.addTween(item,{y:newY + this._dis ,time:0.5});
				//newY = item.y;
				newY += this._dis;
			}
			
			//trace("refresh  count = " + count);
		}
		
		
	}
}